<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/20
 * Time: 9:20
 */
namespace app\index\controller;

use think\Db;
use app\common\lib\RedisManager;

class Order extends Base {



    private $order_model = null;

    private $order_image = null;

    private $task_key_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->order_model = new \app\admin\model\shop\Order;
        $this->order_image = new \app\common\model\order\Image;

        $this->task_key_model = new \app\common\model\shop\task\Key;


    }



    //订单列表php徐然
    public function index(){
        $page      = input('page')?:1;//页数
        $page_size = input('page_size')?:10;//页码数
        $_where["uid"] = $this->uid;
        $_where["task_type"] = array("in",[1,2,3]);
        $list  = $this->order_model
                ->with(["task"=>function($q){$q->field("goods_url,task_id");}])
                ->where($_where)
                ->field("shop_name,task_id,task_type,FROM_UNIXTIME(create_time) as create_time,status,order_sno,order_id,delete_time")
                ->page($page,$page_size)
                ->group("order_id")
                ->order("create_time DESC")
                ->select();
        $status_list = $this->order_model->getStatusName();
        $this->assign("list",$list);
        $this->assign("status_list",$status_list);
        return $this->fetch('shop/orderList');

    }


    //订单列表ajax
    public function orderList(){
        $task_type = input('type');//task_type:1；精刷单，2：隔日单，3：流量单
        $_where = [];//条件
        //订单类型
        switch ($task_type){
            case "mine":
                $_where["task_type"] = array("in",[1,2,3]);
                break;
            case "jinshua":
                $_where["task_type"] = 1;
                break;
            case "liuliang":
                $_where["task_type"] = 3;
                break;
            case "yestoday":
                $_where["task_type"] = 2;
                break;
        }
        $_where["uid"] = $this->uid;
        $page      = input('page')?:1;//页数
        $page_size = input('page_size')?:10;//页码数
        $data["count"] = ceil(($this->order_model->where($_where)->count())/$page_size);
        $data["list"]  =  $this->order_model
            ->with(["task"=>function($q){$q->field("goods_url,task_id");}])
            ->where($_where)
            ->field("shop_name,task_id,task_type,FROM_UNIXTIME(create_time) as create_time,status,order_sno,order_id")
            ->page($page,$page_size)
            ->group("order_id")
            ->order("create_time DESC")
            ->select();
        $data["status_list"] = $this->order_model->getStatusName();
        renderJson("成功获取",1,$data);
    }


    //上传图片的页面
    public function screenShot(){
        $order_id  = input("order_id");//订单id
        $order_info = $this->order_model->where("order_id",$order_id)->find();
        $this->assign("order_info",$order_info);
        return $this->fetch("shop/screenshot");

    }

    //流量单的页面
    public function flower(){
        $order_id  = input("order_id");//订单id
        $order_info = $this->order_model->where("order_id",$order_id)->find();
        $this->assign("order_info",$order_info);
        return $this->fetch("shop/flower");

    }


    //隔日单的图片上传页面
    public function yestodyUpload(){
        $order_id  = input("order_id");//订单id
        $order_info = $this->order_model->where("order_id",$order_id)->find();
        $this->assign("order_info",$order_info);
        return $this->fetch("shop/yestodyUpload");
    }


    //已付款的页面
    public function payed(){
        $order_id  = input("order_id");//订单id
        $this->assign("order_id",$order_id);
        return $this->fetch("shop/payed");
    }

    //好评与追评的页面
    public function haoping(){
        $order_id  = input("order_id");//订单id
        $order_info = $this->order_model->with("task")->where("order_id",$order_id)->find();
        $this->assign("order_info",$order_info);
        return $this->fetch("shop/haoping");
    }


    //订单跳转
    public function orderDetail($order_id=NULL){
        $order_info = $this->order_model->with("task")->where("order_id",$order_id)->find();
        if(!$order_info){//订单有误
            renderJson("订单有误",0);
        }
        $status = $order_info["status"];//订单状态
        $task_type = $order_info["task_type"];//订单类型1：精刷单2：隔日单，3：流量单
        $msg_info = "";
        // *  0=>已拍下，1=》未审核，2=》已审核，3=》已上传，4=》已复审5=》已付款，6=》已下单，7=》待发货，8=》待好评，9=》已发货，10=》已好评，11=》待追评，12=》已追评，13=》已完成
        switch ($status){//订单状态
//            case 0:    //已拍下状态
//                $msg_info = "该订单属于已拍下";
//                renderJson($msg_info,1);
//                break;
            case 0:   //已拍下状态
                $msg_info = "请上传截图";
                if($task_type==2) {
                    $info["url"] = url("order/yestodyUpload");
                }elseif($task_type==1){
                    $info["url"] =  url("order/screenShot");
                }elseif($task_type==3){
                    $info["url"] =  url("order/flower");
                }
                $info["order_id"]  = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 1:    //资料上传未审核
                $msg_info = "资料上传,平台正在为您审核！请稍等";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 2:     //流量单只有0,1,14（隔日单，精刷单有已下状态）
                $info = [];
                $msg_info = "";
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($task_type==1) {
                    $msg_info = "平台已审核通过！等待商家打钱";
                }elseif($task_type==2){
                    $msg_info = "平台已审核！明日继续上传图片";
                    $info["url"] = url("order/screenShot");
                }
                $info["order_id"] =    $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 3:                 //已上传
                if($task_type==1 || $task_type==3){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "资料已上传！请等待复审";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 4:    //平台复审通过
               if($task_type==1 || $task_type==3){
                 renderJson("订单信息有误",0);
               }
                $msg_info    = "平台已复审！请等待商家打钱";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 5://商家打完给派克，已付款状态
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "商家已打钱，请上传已下单成功的截图";
                $info["order_id"] = $order_info["order_id"];
                $info["url"]      = url("order/payed");
                renderJson($msg_info,1,$info);
                break;
            case 6://派克上传资料，已下单资料
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "您已经下单完成！请等待平台审核";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 7://待发货状态（任务类型是精晒单或者隔日单，而且还需要is_need_send是1《需要发货的状态》）
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_need_send==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "平台审核通过！请等待商家发货";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 8://待好评的状态
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_need_send==1 || $order_info["task"]->is_good_evalute==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "平台审核通过！派克请上传好评截图";
                $info["order_id"] = $order_info["order_id"];
                $info["url"]    = url("order/haoping");
                renderJson($msg_info,1,$info);
                break;
            case 9://已发货只有精刷单还有隔日单is_need_send==1的时候
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_need_send==2 || $order_info["task"]->is_good_evalute==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "卖家已经发货！派克请上传好评截图";
                $info["order_id"] = $order_info["order_id"];
                $info["url"]    = url("order/haoping");
                renderJson($msg_info,1,$info);
                break;
            case 10://已好评状态只有精刷单还有隔日单is_good_evalute==1
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_good_evalute==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "您已经好评上传成功了！请等待平台审核";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 11://待追评状态只有精刷单还有隔日单才有这个状态is_review==1才可以
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_good_evalute==2 || $order_info["task"]->is_review==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "平台审核已通过！请上传追评截图";
                $info["order_id"] = $order_info["order_id"];
                $info["url"] = url("order/haoping");
                renderJson($msg_info,1,$info);
                break;
            case 12://已追评等待平台审核
                if($task_type==3){
                    renderJson("订单信息有误",0);
                }
                if($order_info["task"]->is_good_evalute==2 || $order_info["task"]->is_review==2){
                    renderJson("订单信息有误",0);
                }
                $msg_info    = "您已上传追评截图,等待平台审核";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;
            case 13:
                $msg_info    = "您已经完成该订单";
                $info["order_id"] = $order_info["order_id"];
                renderJson($msg_info,1,$info);
                break;


        }                                                           










    }


    //订单取消=======任务加一==========订单软删除
    public function orderCancle($order_id=NULL){
        $order_info = Db::name("shop_order")->where("order_id",$order_id)->find();
        if(!$order_info){//订单有误
            renderJson("订单有误",0);
        }
        Db::startTrans();
        //软删除shop_order
        try {
            $op = $this->order_model->destroy($order_id);
            if($op){
                Db::name("shop_task_key")->where("task_id",$order_info["task_id"])->setInc("left_num",1);
                if($order_info["room"]=="1") {
                    Db::name("shop_task_key")->where("task_id", $order_info["task_id"])->setInc("left_task_num_one", 1);
                }elseif($order_info["room"]=="2"){
                    Db::name("shop_task_key")->where("task_id", $order_info["task_id"])->setInc("left_task_num_tow", 1);
                }
                Db::name("shop_task")->where("task_id",$order_info["task_id"])->setInc("left_task_num",1);
                Db::commit();
                renderJson("取消成功",1);
            }else{
                Db::rollback();
            }
        }catch(\Exception $e){
            renderJson($e->getMessage(),0);
        }









    }




    //下单

    /**
     *
     * task_key_id 减一=》task减一
     *
     * order表加一
     *
     *
     */
    public function orderDown(){
        //console.log(id);
        $uid = $this->uid;
        $room = $this->userAttribute["room"];
        $task_key_id = input("task_key_id");
        $task_key_info = $this->task_key_model->with(["task"=>function($q){$q->with("shop");}])->where("take_key_id",$task_key_id)->find();//当前关键字的任务

        $left_num = 0;//剩余数量
        $field = "";//字段取值
        if($room==1){
            if($task_key_info["left_task_num_one"]<=0){
                renderJson("该任务已经没有订单了",0);
            }
            $left_num =  $task_key_info["left_task_num_one"];
            $field = "left_task_num_one";
        }
        if($room==2){
            if($task_key_info["left_task_num_two"]<=0){
                renderJson("该任务已经没有订单了",0);
            }
            $left_num =  $task_key_info["left_task_num_two"];
            $field = "left_task_num_two";
        }
        //剩余数量大于0
        if($left_num>0){
           Db::startTrans();
            try{
                $real_money = 0.00;
                if($task_key_info["task"]->service_change_price){
                    $real_money = $task_key_info["task"]->service_change_price;
                }else{
                    $real_money = $task_key_info["task"]->beat_price;
                }
                $data_order = [
                    "uid" => $uid,
                    "task_id"=>$task_key_info["task_id"],
                    "order_sno"=>get_only_order_no(),
                    "tao_bao_order_no"=>"",
                    "create_time"=>time(),
                    "update_time"=>time(),
                    "status"=>0,
                    "order_state"=>0,
                    "shop_id"=>$task_key_info["task"]->shop_id,
                    "shop_name"=>$task_key_info["task"]->shop->shop_name,
                    "tao_bao_no"=>$this->userInfo["tao_bao_no"],
                    "beat_price"=>$task_key_info["task"]->beat_price,
                    "service_change_price"=>$task_key_info["task"]->service_change_price,
                    "task_type"=>$task_key_info["task"]->task_type,
                    "real_money"=>$real_money,
                    "shop_admin_id"=>$task_key_info["task"]->shop_admin_id,
                    "take_key_id"=>$task_key_id,
                     "key_word"=>$task_key_info["key_word"],
                     "goods_sno"=>$task_key_info["task"]->goods_sno,
                    "flag_remarks"=>"",
                    "author"=>0,
                    "back_time"=>0,
                    "flag_time"=>0,
                    "flag_pass_time"=>0
                ];
                $op = $this->order_model->save($data_order);
                if($op){
                    Db::name("shop_task")->where("task_id",$task_key_info["task_id"])->setDec("left_task_num",1);//任务数量
                    Db::name("shop_task_key")->where("take_key_id",$task_key_id)->setDec("left_num",1);//关键字任务剩余数量
                    Db::name("shop_task_key")->where("take_key_id",$task_key_id)->setDec($field,1);//关键字放简剩余数量
                    Db::commit();
                    renderJson("抢单成功",1);
                }else{
                    renderJson("抢单失败",0);
                }
            }catch (\Exception $e){

                renderJson($e->getMessage(),0);

            }


        }











    }


    /**
     * (1)上传图片
     *
     *  订单状态是status 会有上传图片操作
     *
     *
     *  status=》0（已拍下）
     *
     *  可以上传图片派克截图
     *
     *
     *  下面的状态（隔日单还有经刷单）是可以上传图片
     *
     *  status=》2（状态为2的时候，只有隔日单才可以上传图片）
     *
     *
     *
     *  //商家付款的时候操作（已付款）
     *  status=》5（派克上传下单资料《精刷单还有隔日单都可以操作》）
     *
     *
     *  //已发货 和  待好评(上传好评图片)
     *  status=》9 和 status=》8《精刷单还有隔日单都可以操作》
     *
     *
     *  //待追评（上传追评）
     *  status=》 11 《精刷单还有隔日单都可以操作》
     *
     *
    **/

    public function orderUpload(){
            $order_id = input("order_id");//订单id
            $order_info = $this->order_model->with("task")->where("order_id",$order_id)->find();
            if(!$order_info){
                renderJson("订单信息有误",0);
            }
            $status = $order_info["status"];
            if(!in_array($status,[0,2,5,8,9,11])){
                renderJson("该订单状态不可以上传资料！",0);
            }
            switch ($status){
                case 0://派克上传上传截图
                case 2://平台审核(只有隔日单才可以上传)
                    if($status==2 && $order_info["task_type"]!=2){//订单信息有误
                        renderJson("订单信息有误",0);
                    }

                    Db::startTrans();
                    try {
                        $orderImage = [];

                        if($order_info["task_type"]==3){

                            $orderImage = [
                                "order_id" => $order_id,
                                "auth_status" => 0,
                                "enjoyed_img" => input("like"),
                                "main_img_up" => input("main_up"),
                                "main_img_mid" => input("main_center"),
                                "main_img_low" => input("main_low"),
                                "type" => 3
                            ];

                        }elseif($order_info["task_type"]==1 || $order_info["task_type"]==2) {
                            $_status = 1;
                            $payment_img = input("waitpay");
                            if($status==2){
                                $_status = 2;
                            }
                            if($order_info["task_type"]==2){
                                $payment_img = "";
                            }
                            $orderImage = [
                                "order_id" => $order_id,
                                "auth_status" => 0,
                                "payment_img" => $payment_img,
                                "enjoyed_img" => input("like"),
                                "one_house" => input("shop1"),
                                "two_house" => input("shop2"),
                                "three_house" => input("shop3"),
                                "pro_search_img" => input("seach"),
                                "main_cat_img" => input("main_cat"),
                                "main_img_up" => input("main_up"),
                                "main_img_mid" => input("main_center"),
                                "main_img_low" => input("main_low"),
                                "pro_collect_img" => input("pro_collection"),
                                "store_collect_img" => input("shop"),
                                "fake_chat_img" => input("chat"),
                                "tao_bao_order_no" => input("ordersn"),
                                "type"=>$order_info["task_type"],
                                "status_type"=>$_status
                            ];
                        }
                        $op = $this->order_image->save($orderImage);
                        if($op){

                            if($order_info["task_type"]==1 || $order_info["task_type"]==3) {

                                $this->order_model->save(["status" => 1, "tao_bao_order_no" => input("ordersn")], ["order_id" => $order_id]);

                            }

                            if($order_info["task_type"]==2 && $status==2){

                                $this->order_model->save(["status"=>3,"tao_bao_order_no" => input("ordersn")],["order_id"=>$order_id]);
                            }else{
                                $this->order_model->save(["status"=>1,"tao_bao_order_no" => input("ordersn")],["order_id"=>$order_id]);
                            }


                            Db::commit();
                            renderJson("完成上传",1);

                        }else{
                            Db::rollback();
                        }
                    } catch (\Exception $e){
                        renderJson($e->getMessage(),0);
                    }

                    break;
                case 5:
                    if($order_info["task_type"]==3){
                        renderJson("订单信息有误",0);
                    }
                    Db::startTrans();
                    try {
                        $orderImage = [
                            "payed_img"=>input("payed"),
                        ];
                        $op = $this->order_image->save($orderImage,["order_id"=>$order_id]);
                        if($op){
                            $this->order_model->save(["status"=>6],["order_id"=>$order_id]);
                            Db::commit();
                            renderJson("完成上传",1);
                        }else{
                            renderJson("更新有误",0);
                        }
                    } catch (\Exception $e){
                        renderJson($e->getMessage(),0);
                    }
                    break;
                case 8:
                    if($order_info["task_type"]==3){
                        renderJson("订单信息有误",0);
                    }
                    if($order_info["task"]->is_need_send!=2 || $order_info["task"]->is_good_evalute!=1){
                        renderJson("订单信息有误",0);
                    }
                    Db::startTrans();
                    try {
                        $praise = [
                            "order_id"=>$order_id,
                            "praise_img"=>input("praise_img"),
                            "praise_cat_img"=>input("praise_cat_img"),
                            "on_time"=>time(),
                            "update_time"=>time()
                        ];
                        $op = Db::name("praise")->insert($praise);
                        if($op){
                            $this->order_model->save(["status"=>10],["order_id"=>$order_id]);
                            Db::commit();
                            renderJson("完成上传",1);
                        }else{
                            Db::rollback();
                        }
                    } catch (\Exception $e){
                        renderJson($e->getMessage(),0);
                    }
                    break;
                case 9:
                    if($order_info["task_type"]==3){
                        renderJson("订单信息有误",0);
                    }
                    if($order_info["task"]->is_need_send!=1 || $order_info["task"]->is_good_evalute!=1){
                        renderJson("订单信息有误",0);
                    }
                    Db::startTrans();
                    try {
                        $praise = [
                            "order_id"=>$order_id,
                            "praise_img"=>input("praise_img"),
                            "praise_cat_img"=>input("praise_cat_img"),
                            "on_time"=>time(),
                            "update_time"=>time()
                        ];
                        $op = Db::name("praise")->insert($praise);
                        if($op){
                            $this->order_model->save(["status"=>10],["order_id"=>$order_id]);
                            Db::commit();
                            renderJson("完成上传",1);
                        }else{
                            Db::rollback();
                        }
                    } catch (\Exception $e){
                        renderJson($e->getMessage(),0);
                    }
                    break;
                case 11:
                    if($order_info["task_type"]==3) {
                        renderJson("订单信息有误", 0);
                    }
                    Db::startTrans();
                    try {
                        $review = [
                            "order_id"=>$order_id,
                            "praise_img"=>input("praise_img"),
                            "praise_cat_img"=>input("praise_cat_img"),
                            "on_time"=>time(),
                            "update_time"=>time(),
                        ];
                        $op = Db::name("review")->insert($review);
                        if($op){
                            $this->order_model->save(["status"=>12],["order_id"=>$order_id]);
                            Db::commit();
                            renderJson("完成上传",1);
                        }else{
                            Db::rollback();
                        }

                    } catch (\Exception $e){
                        renderJson($e->getMessage(),0);
                    }
                    break;
            }

    }











}